﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.IO.IsolatedStorage;
using System.Xml.Linq;
using System.IO;
using System.Linq;
using QuickTasks.ViewModels.Properties;
using System.Collections.Generic;
using QuickTasks.GeocodeService;
using LocalSettings;
using Microsoft.Phone.Controls;
using QuickTasks.ViewModels.Tables;
using System.Collections.ObjectModel;
namespace QuickTasks.ViewModels.Data
{
    public class AddAddress
    {
        #region Global
       
        int cnt = 0;

        #endregion

        #region Events


        public string SaveAddress(string name, string address, string city, string state, string country, string zipcode, string id)
        {
            string type = IsolatedSettings.getSettingsStringValue("type");
            string hisId = "";
            if (type != "Edit" && type != "new")
            {
                using (QuickTaskDataContext context = new QuickTaskDataContext(App.ConnectionString))
                {
                    var addressid1 = from i in context.Add select i;
                    foreach (var rep1 in addressid1)
                    {
                        if (rep1.address.Contains(address))
                        {
                            cnt++;
                        }

                    }
                }

            }

           

            if (cnt == 0)
            {
                if (type == "Edit")
                {
                    try
                    {
                        IsolatedSettings.saveSettings("type", "");
                        using (QuickTaskDataContext context = new QuickTaskDataContext(App.ConnectionString))
                        {
                            IQueryable<QuickTasks.ViewModels.Tables.Address> addressid = from i in context.Add where i.id.ToString() == id select i;
                            QuickTasks.ViewModels.Tables.Address add = addressid.FirstOrDefault();
                            {
                                add.name = name;
                                add.address = address;
                                add.city = city;
                                add.state = state;
                                add.country = country;
                                add.zipcode = zipcode;
                            };
                            context.SubmitChanges();
                        }
                    }
                    catch (Exception ex)
                    {
                        Exceptions.SaveOrSendExceptions("Exception in SaveAddress method In AddAddress file", ex);
                    }
                }

                else
                {
                    try
                    {
                        IsolatedSettings.saveSettings("type", "");
                        int tid = 0;
                        using (QuickTaskDataContext context = new QuickTaskDataContext(App.ConnectionString))
                        {
                            var xquery = from a in context.Add
                                         orderby a.id descending
                                         select a.id;
                            if (xquery.Count() <= 0)
                            {
                                tid = 1;
                            }
                            else
                            {
                                var xquery1 = from a in context.Add
                                              orderby a.id descending
                                              select a;
                                foreach (var query in xquery1)
                                {
                                    tid = Convert.ToInt32(query.id) + 1;
                                    hisId = tid.ToString();
                                    break;
                                }
                            }
                            QuickTasks.ViewModels.Tables.Address add = new QuickTasks.ViewModels.Tables.Address()
                             {
                                 id = tid,
                                 name = name,
                                 address = address,
                                 city = city,
                                state = state,
                                 country = country,
                                 zipcode = zipcode
                             };
                            context.Add.InsertOnSubmit(add);
                            context.SubmitChanges();
                        }
                    }
                    catch(Exception ex)
                    {
                        Exceptions.SaveOrSendExceptions("Exception in SaveAddress method In AddAddress file", ex);
                    }
                }
            }
            return hisId;
        }
        public List<AddressProp> LoadAddressDetails()
        {
            List<AddressProp> objAddaddress = new List<AddressProp>();
            using (QuickTaskDataContext context = new QuickTaskDataContext(App.ConnectionString))
            {
                var addrequery = from a in context.Add select a;
                foreach (var addresslist in addrequery)
                {
                    AddressProp objAddressProp = new AddressProp();
                    objAddressProp.Id = addresslist.id.ToString();
                    objAddressProp.Name = addresslist.name;
                    objAddressProp.City = addresslist.address;
                    objAddaddress.Add(objAddressProp);
                }
            }
            
            return objAddaddress;
        }
        public ObservableCollection<AddressProp> loadCityData(string id)
        {

            ObservableCollection<AddressProp> objAddaddress = new ObservableCollection<AddressProp>();
            using (QuickTaskDataContext context = new QuickTaskDataContext(App.ConnectionString))
            {
                var citylist = from c in context.Add  select c;
                foreach (var citylistdata in citylist)
                {
                    AddressProp objAddressProp = new AddressProp();
                    objAddressProp.Id = citylistdata.id.ToString();
                    objAddressProp.Name = citylistdata.name;
                    objAddressProp.City = citylistdata.address;
                   
                    if (citylistdata.id.ToString() == "1")
                    {
                        objAddressProp.visible1 = Visibility.Collapsed;
                    }
                    else
                    {
                        objAddressProp.visible1 = Visibility.Visible;
                    }
                    if (citylistdata.id.ToString() == id)
                        objAddressProp.colour = Application.Current.Resources["PhoneAccentBrush"] as SolidColorBrush;
                    else
                    {
                        PhoneApplicationPage X = new PhoneApplicationPage();
                        if ((Color)X.Resources["PhoneBackgroundColor"] == Colors.White)
                        {
                            objAddressProp.colour = new SolidColorBrush(Colors.White);
                        }
                        else
                        {
                            objAddressProp.colour = new SolidColorBrush(Colors.White);
                        }
                    }
                    objAddaddress.Add(objAddressProp);
                }
            }
            return objAddaddress;
        }
        #endregion
    }
}
